//
//  DZMRMLeftView.swift
//  DZMeBookRead
//
//  Created by 邓泽淼 on 2017/5/15.
//  Copyright © 2017年 DZM. All rights reserved.
//

import UIKit

class DZMRMLeftView: DZMRMBaseView, DZMSegmentedControlDelegate{
    
    /// topView
    lazy var topView = DZMSegmentedControl()
    
    /// contentView
    lazy var contentView = UIView()
    
    /// UITableView
    lazy var chapterTableView = UITableView(frame: CGRect.zero, style: .plain)
    lazy var markTableView = UITableView(frame: CGRect.zero, style: .plain)

    /// 类型 0: 章节 1: 书签
    private var type = 0
    
    override func addSubviews() {
        super.addSubviews()
        
        // contentView
        self.contentView.backgroundColor = UIColor.readMenu.bg
        self.addSubview(self.contentView)
        
        // UITableView
        self.chapterTableView.register(ChapterCell.self, forCellReuseIdentifier: "ChapterCell")
        self.chapterTableView.backgroundColor = UIColor.clear
        self.chapterTableView.separatorStyle = .none
        self.chapterTableView.contentInset = UIEdgeInsets(top: 20.0, left: 0, bottom: 0, right: 0)
        self.chapterTableView.delegate = self
        self.chapterTableView.dataSource = self
        self.contentView.addSubview(self.chapterTableView)
        
        self.markTableView.register(MarkCell.self, forCellReuseIdentifier: "MarkCell")
        self.markTableView.contentInset = UIEdgeInsets(top: 20.0, left: 0, bottom: 0, right: 0)
        self.markTableView.backgroundColor = UIColor.clear
        self.markTableView.separatorStyle = .none
        self.markTableView.delegate = self
        self.markTableView.dataSource = self
        self.contentView.addSubview(self.markTableView)
        
        // topView
        self.topView.delegate = self
        self.topView.normalTitles = ["章节","书签"]
        self.topView.selectTitles = ["章节","书签"]
        self.topView.horizontalShowTB = false
        self.topView.backgroundColor = UIColor.clear
        self.topView.normalTitleColor = UIColor.cj.color_6
        self.topView.selectTitleColor = UIColor.cj.color_2
        self.topView.setup()
        self.contentView.addSubview(self.topView)
        
        self.contentView.snp.makeConstraints { (make) in
            make.right.equalTo(self.snp.left)
            make.width.equalTo(self.snp.width).multipliedBy(0.7)
            if UIScreen.main.bounds.height == 812.0
                || UIScreen.main.bounds.height == 896.0 {
                make.top.equalToSuperview().offset(44.0)
                make.bottom.equalToSuperview().offset(-34.0)
            } else {
                make.top.equalToSuperview()
                make.bottom.equalToSuperview()
            }
        }
        
        self.topView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(45.0)
        }
        
        self.chapterTableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(self.topView.snp.bottom)
        }
        
        self.markTableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(self.topView.snp.bottom)
        }
        self.markTableView.isHidden = true
    }
    
    // MARK: -- DZMSegmentedControlDelegate
    func segmentedControl(segmentedControl: DZMSegmentedControl, clickButton button: UIButton, index: NSInteger) {
        self.type = index
        if self.type == 0 {
            self.chapterTableView.reloadData()
            self.chapterTableView.isHidden = false
            self.markTableView.isHidden = true
        } else {
            self.markTableView.reloadData()
            self.chapterTableView.isHidden = true
            self.markTableView.isHidden = false
        }
    }
}

extension DZMRMLeftView : UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        guard let readMenu_ = self.readMenu else { return 0 }
        
        if type == 0 { // 章节
            return readMenu_.vc.book.chapters.count
        }else{ // 书签
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if tableView == self.chapterTableView { //章节
            let cell = tableView.dequeueReusableCell(withIdentifier: "ChapterCell") as! ChapterCell
            if let book = self.readMenu?.vc.book {
                cell.refresh(book: book, chapterIndex: indexPath.row)
            }
            return cell
        } else { //书签
            let cell = tableView.dequeueReusableCell(withIdentifier: "MarkCell") as! MarkCell
            guard let readMenu_ = self.readMenu else { return cell }
            
            return cell
        }
    }
}

extension DZMRMLeftView : UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if self.type == 0 { // 章节
            return 40.0
        } else { // 书签
            return 80.0
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        guard let readMenu_ = self.readMenu else { return }
        
        if self.type == 0 {
            // 章节
            readMenu_.delegate?.readMenuClickChapterList?(readMenu: readMenu_, index: indexPath.row)
        } else {
            // 书签
//            readMenu_.delegate?.readMenuClickMarkList?(readMenu: readMenu_, readMarkModel: readMenu_.vc.cjReadModel.marks[indexPath.row])
        }
        
        // 隐藏
        readMenu_.leftView(isShow: false, complete: nil)
    }
    
    // MARK: -- 删除操作
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        
        if self.type == 0 { // 章节
            return false
        }else{ // 书签
            return true
        }
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
//        self.readMenu?.vc.cjReadModel.deleteReadmark(readMark: nil, index: indexPath.row,callback: { [weak self] in
//            self?.markTableView.reloadData()
//        })
    }
}

extension DZMRMLeftView {
    
    class ChapterCell : UITableViewCell {
        
        private lazy var labelName = UILabel()
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.backgroundColor = UIColor.clear
            self.contentView.addSubview(self.labelName)
            self.labelName.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(15.0)
                make.right.equalToSuperview().offset(-15.0)
                make.centerY.equalToSuperview()
            }
            
            let line = UIView()
            line.backgroundColor = UIColor(red: 0x40 / 255.0, green: 0x40 / 255.0, blue: 0x40 / 255.0, alpha: 1)
            self.contentView.addSubview(line)
            line.snp.makeConstraints { (make) in
                make.height.equalTo(0.5)
                make.left.equalToSuperview().offset(15.0)
                make.right.equalToSuperview().offset(-15.0)
                make.bottom.equalToSuperview()
            }
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        func refresh(book:CLBook,chapterIndex:Int) {
            if book.chapters.count > chapterIndex {
                let chapter = book.chapters[chapterIndex]
                self.labelName.text = chapter.name
                
                if chapter.code == "2" {
                    self.labelName.textColor = UIColor.cj.color_7
                } else {
                    self.labelName.textColor = UIColor.cj.color_6
                }
                
//                if let recordId = book.record.epubChapter?.id,recordId == chapter.id {
//                    self.labelName.textColor = UIColor.cj.color_2
//                }
                
                self.labelName.font = UIFont.system._14
            }

        }
    }
    
    class MarkCell : UITableViewCell {
        
        private lazy var labelName = UILabel()
        private lazy var labelTime = UILabel()
        private lazy var labelContent = UILabel()
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.backgroundColor = UIColor.clear
            
            self.contentView.addSubview(self.labelName)
            self.labelName.font = UIFont.system._12
            self.labelName.textColor = UIColor.cj.color_6
            self.labelName.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(15.0)
                make.right.equalToSuperview().offset(-75.0)
                make.top.equalToSuperview().offset(10.0)
            }
            
            self.contentView.addSubview(self.labelTime)
            self.labelTime.font = UIFont.system._12
            self.labelTime.textColor = UIColor.cj.color_6
            self.labelTime.snp.makeConstraints { (make) in
                make.right.equalToSuperview().offset(-15.0)
                make.top.equalToSuperview().offset(10.0)
            }
            
            self.contentView.addSubview(self.labelContent)
            self.labelContent.font = UIFont.system._12
            self.labelContent.textColor = UIColor.cj.color_6
            self.labelContent.numberOfLines = 0
            self.labelContent.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(20.0)
                make.right.equalToSuperview().offset(-15.0)
                make.top.equalTo(self.labelName.snp.bottom).offset(10.0)
                make.bottom.equalToSuperview().offset(-10.0)
            }
            
            let line = UIView()
            line.backgroundColor = UIColor(red: 0x40 / 255.0, green: 0x40 / 255.0, blue: 0x40 / 255.0, alpha: 1)
            self.contentView.addSubview(line)
            line.snp.makeConstraints { (make) in
                make.height.equalTo(0.5)
                make.left.equalToSuperview().offset(15.0)
                make.right.equalToSuperview().offset(-15.0)
                make.bottom.equalToSuperview()
            }
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
//        func refresh(mark:CJMark) {
//            let name = mark.name
//            let time =  mark.create_time.desString()
//            let content = mark.content
//            self.labelName.text = name
//            self.labelTime.text = time
//            self.labelContent.text = content
//        }
    }

}
